﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace TimeTravel.Extentions
{
    /// <summary>
    /// DataReader扩展
    /// </summary>
    public static class DataReaderExt
    {
        /// <summary>
        /// 获取布尔值类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetBoolean(this IDataReader reader, string key)
        {
            object obj2;
            bool flag;
            return ((reader.TryGetObject(key, out obj2) && bool.TryParse(obj2.ToString(), out flag)) && flag);
        }

        /// <summary>
        /// 获取字节类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static byte GetByte(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToByte(obj2);
            }
            return 0;
        }

        /// <summary>
        /// 获取日期类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DateTime GetDateTime(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToDateTime(obj2);
            }
            return new DateTime();
        }

        /// <summary>
        /// 获取双精度型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static double GetDouble(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToDouble(obj2);
            }
            return 0.0;
        }

        /// <summary>
        /// 获取枚举类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetEnum<T>(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return (T)Enum.Parse(typeof(T), obj2.ToString(), true);
            }
            return default(T);
        }

        /// <summary>
        /// 获取整形数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetInt32(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToInt32(obj2);
            }
            return 0;
        }

        /// <summary>
        /// 获取长整形数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long GetInt64(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToInt64(obj2);
            }
            return 0L;
        }

        /// <summary>
        /// 获取单精度类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static float GetSingle(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return Convert.ToSingle(obj2);
            }
            return 0f;
        }

        /// <summary>
        /// 获取字符串类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetString(this IDataReader reader, string key)
        {
            object obj2;
            if (reader.TryGetObject(key, out obj2))
            {
                return obj2.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取无符号整形类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static uint GetUInt32(this IDataReader reader, string key)
        {
            return (uint)reader.GetInt64(key);
        }

        /// <summary>
        /// 获取无符号长整形类型数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static ulong GetUInt64(this IDataReader reader, string key)
        {
            return (ulong)reader.GetInt64(key);
        }


        private static bool TryGetObject(this IDataReader reader, string key, out object obj)
        {
            bool flag;
            try
            {
                obj = reader[key];
                flag = !(obj is DBNull);
            }
            catch (Exception)
            {
                throw new Exception("未知的数据列名：" + key);
            }
            return flag;
        }
    }
}
